package ee.ttu.rakarh.commands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.ttu.rakarh.model.data.dao.ItemDAO;
import ee.ttu.rakarh.model.data.dao.ItemDAOImpl;
import ee.ttu.rakarh.model.data.dao.ItemStoreDAO;
import ee.ttu.rakarh.model.data.dao.ItemStoreDaoImpl;
import ee.ttu.rakarh.model.data.dao.StoreDAO;
import ee.ttu.rakarh.model.data.dao.StoreDAOImpl;
import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.ItemStore;
import ee.ttu.rakarh.model.data.entities.Store;
import ee.ttu.rakarh.model.form.ItemStoreForm;
import ee.ttu.rakarh.services.ItemStoreService;

public class AddToStoreCommand implements Command {
	public static final Logger LOG = Logger.getLogger(AddToStoreCommand.class);
	@SuppressWarnings("null")
	@Override
	public Boolean execute(HttpServletRequest request,
			HttpServletResponse response) {
		int storeId;
		HashMap<String, String> errors = new HashMap<String, String>();//lol
		double newItemCount = 0;
		
		LOG.info("ADD_TO_STORE_COMMAND");
		
				
		try{
			String storeIdParam = request.getParameter("ladu");
			
			storeId = Integer.parseInt(storeIdParam);
			
			LOG.info("ladu = " + request.getParameter("ladu"));
			storeId = Integer.parseInt(request.getParameter("ladu"));
			//ItemStoreForm itemStoreForm = new ItemStoreForm();
			//itemStoreForm.setId(storeId);
			
			ItemDAO itemImpl = new ItemDAOImpl();
			
			LOG.info("itemId = " + request.getParameter("itemId"));
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			Item item = itemImpl.getItemById(itemId);
			//itemStoreForm.setItem(item);
			
			//ItemStoreDAO itemStoreImpl = new ItemStoreDaoImpl();
			//ItemStore itemStore = itemStoreImpl.getItemStore(item);
			double oldItemCount = 0;
			//if(itemStore != null) oldItemCount = itemStore.getItemCount();
			
			LOG.info("kogus = " + request.getParameter("kogus"));
			
			String kogusParameter = request.getParameter("kogus");
			Boolean goodInt = false;
			int count = 0;
			if(kogusParameter == null || kogusParameter == ""){
				errors.put("kogus", "empty!!");
			}else{
				try{
				newItemCount = Double.parseDouble(kogusParameter);
				count = Integer.parseInt(kogusParameter);
				//itemStoreForm.setItemCount(oldItemCount + newItemCount);
				if(count < 0)
					throw new Exception();
				}catch (NumberFormatException e) {
					LOG.info("not a number");
				}
			}
			StoreDAO storeImpl = new StoreDAOImpl();
			Store store = storeImpl.getStore(storeId);
			LOG.info("bug1");
			//itemStoreForm.setStore(store);
			LOG.info("bug2");
			ItemStoreService itemStoreService = new ItemStoreService();
			itemStoreService.updateItemStore(item, store, count);
			//ItemStore updatedItemStore = itemStoreForm.convertToModel();
			LOG.info("bug3");
		//	itemStoreImpl.updateItemStore(updatedItemStore);
		}catch(Exception e){
			errors.put("kogus", "can not be negative or 0");
			LOG.error(e);
		}
		
		
		String errorsString = errors.toString();
		errorsString = errorsString.substring(1, errorsString.length() - 1);
		request.setAttribute("result", errors.size() == 0 ? "" : errorsString.trim());	
		LOG.info("errors: "+ errorsString.trim());

		
		return true;
	}

}
